import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'  // 此包把token保存到本地中
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  // 配置为vuex的插件
  plugins: [createPersistedState()],
  state: {
    token: "",
    userInfo: null
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
    },
    updateUserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    async getUserInfo (context) {
      const res = await axios.get('/my/userinfo') 
      if (res.data.code === 0) {
        // 获取成功,更新状态
        context.commit('updateUserInfo', res.data.data)
      }
    }
  },
  modules: {
  }
})
